package com.jsu.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jsu.information.mapper.InformationLikeMapper;
import com.jsu.information.mapper.InformationMapper;
import com.jsu.information.service.InformationService;
import org.example.model.common.dtos.ResponseResult;
import org.example.model.common.enums.WebHttpCodeEnum;
import org.example.model.information.pojo.Information;
import org.example.model.information.pojo.InformationLike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private InformationLikeMapper informationLikeMapper;

    @Override
    public ResponseResult getUrl(Integer informationId) {
        Information information = informationMapper.selectById(informationId);
        if (Objects.isNull(information) || StringUtils.isEmpty(information.getInformationUrl())){
            throw new RuntimeException("文件不存在！");
        }
        String informationUrl = information.getInformationUrl();

        return ResponseResult.okResult(informationUrl);
    }

    @Override
    public ResponseResult addInfo(Information information) {
        informationMapper.insert(information);
        return ResponseResult.okResult("新增成功");
    }

    @Override
    public ResponseResult delInfo(Integer informationId) {
        informationMapper.deleteById(informationId);
        return ResponseResult.okResult("删除成功");
    }

    @Override
    public ResponseResult getAllInfos(List<Integer> informationId) {
        if (Objects.isNull(informationId) || informationId.size() == 0){
            return ResponseResult.errorResult(WebHttpCodeEnum.DATA_NOT_EXIST);
        }
        List<Information> information = informationMapper.selectBatchIds(informationId);

        return ResponseResult.okResult(information);
    }


    @Override
    public ResponseResult addViews(Integer informationId) {
        Information information = informationMapper.selectById(informationId);
        if (Objects.isNull(information)){
            throw new RuntimeException("文件不存在！");
        }
        information.setViews(information.getViews()+1);//新增
        informationMapper.updateById(information);
        return ResponseResult.okResult("更新浏览量成功");
    }

    @Override
    public ResponseResult addLikes(Integer informationId,Integer userId) {
        if (informationId==null || userId == null){
            throw new RuntimeException("传递的参数有空值，无法进行查询");
        }
        Integer data = (Integer) getUserLikes(informationId, userId).getData();
        if(data==1){
            //表示已经点过赞
            return ResponseResult.errorResult(400,"点赞失败，无法重复点赞");
        }
        InformationLike informationLike = new InformationLike();
        informationLike.setInformationId(informationId);
        informationLike.setUserId(userId);
        informationLike.setCreateTime(new Date());
        informationLike.setDeleteFlag(0);
        informationLikeMapper.insert(informationLike);

        return ResponseResult.okResult("点赞成功");
    }

    @Override
    public ResponseResult getUserLikes(Integer informationId, Integer userId) {
        if (informationId==null || userId == null){
            throw new RuntimeException("传递的参数有空值，无法进行查询");
        }
        LambdaQueryWrapper<InformationLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InformationLike::getInformationId,informationId)
                .eq(InformationLike::getUserId,userId);
        InformationLike informationLike = informationLikeMapper.selectOne(lambdaQueryWrapper);
        //为空表示没有
        if (Objects.isNull(informationLike)){
            return ResponseResult.okResult(0);
        }
        return ResponseResult.okResult(1);
    }

    @Override
    public ResponseResult delUserLikes(Integer informationId, Integer userId) {
        if (informationId==null || userId == null){
            throw new RuntimeException("传递的参数有空值，无法进行查询");
        }
        LambdaQueryWrapper<InformationLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InformationLike::getInformationId,informationId)
                .eq(InformationLike::getUserId,userId);
        InformationLike informationLike = informationLikeMapper.selectOne(lambdaQueryWrapper);
        if (!Objects.isNull(informationLike)){
            informationLike.setDeleteFlag(1);//设置为已删除
            informationLikeMapper.updateById(informationLike);
            return ResponseResult.okResult(1);
        }

        return ResponseResult.errorResult(400,"用户未点赞");
    }


}
